package simulator;


/**
 * A frame of the buffer cache contains a block of data. It also has:
 * a time stamp, to indicate the last time it was accessed
 * a bit to determine if it is dirty
 * a bit to determine if it is the most up to date version of the block (UBJ only)
 * a bit to determine if it has been committed in-place i.e. frozne (UBJ only)
 * @author Paul
 *
 */
public class Frame {

	/**
	 * reference to the block that is in this frame
	 */
	Block block;
	
	/**
	 * last time this frame was referenced
	 */
	int timeStamp; 
	
	/**
	 * true if block has been modified but the change has not yet been written to disk
	 */
	boolean isDirty; 
	
	
	/**
	 * true if block is most recent version of the block
	 */
	boolean isUptodate; 
	
	/**
	 * true if this block has been committed in place (UBJ only). Do not write
	 */
	boolean isFrozen; 
	
	/**
	 * Reference to the copy that was most recently created
	 */
	int latestFrame;
	
	int prevFrame;
	
	
	public Frame()
	{
		resetFlags();
		
		latestFrame = -1;
		prevFrame = -1;
		timeStamp = -1;
	}
	
	/**
	 * Get the data of the frame's block.
	 * Should only be called on a cache hit, so we update the time stamp
	 * @return
	 */
	public int getData(){
		return block.getData(); //return the block data
	}
	
	/**
	 * Set the data in the frame.
	 * @param data
	 */
	public void setData(int data){
		block.setData(data);
		isDirty = true;
	}
	
	public String getName(){
		return block.getName();
	}
	
	public void outOfDate(int latestFrame)
	{
		this.latestFrame = latestFrame;
		this.isUptodate = false;
	}
	
	public void resetFlags()
	{
		isDirty = false;
		isUptodate = false;
		isFrozen = false;
	}
	
	public boolean getIsDirty()
	{
		return isDirty;
	}
	
	public void setIsDirty(boolean isDirty)
	{
		this.isDirty = isDirty;
	}
	
	public boolean getIsUptodate()
	{
		return isUptodate;
	}
	
	public void setIsUptodate(boolean isUpdatetodate)
	{
		this.isUptodate = isUpdatetodate;
	}
	
	public Block getBlock()
	{
		return block;
	}
}
